<?php
/**
 * 用户类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年12月6日 上午9:21:37 
 * 类说明
 */
class UserAction extends BaseAction{
	
	
	/**
	 * 我的账户
	 */
	public function accounts(){
		$UserDetails = M('UserDetails');$User = M('User');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		$where['user_id'] = $userId;
		$where['flag'] = 1;
		$where['create_time'] = array('gt',time()-(30*86400));//读取最近一个月的记录
		import('ORG.Util.Page');
		$count = $UserDetails->where($where)->count();
		$Page = new Page($count,5);
		$show = $Page->new_show();
		$list = $UserDetails->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('create_time desc')->select();
		$balance = $User->where('id = '.$userId)->getField('balance');
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('balance',formatPrice($balance));
		$this->display();
	}
	
	/**
	 * 充值
	 */
	public function recharge(){
		$RechargeSetting = M('RechargeSetting');$User = M('User');
		$userId = $_SESSION['HS_USER_ID'];
		$data = $RechargeSetting->where('flag = 1')->field('id,recharge_price as rechargePrice,give_price as givePrice')->select();
		$balance = $User->where('id = '.$userId)->getField('balance');
		$this->assign('data',$data);
		$this->assign('balance',$balance);
		$this->display();
	}
	
	/**
	 * 推广列表
	 */
	public function extendList(){
		$User = M('User');$UserDetails = M('UserDetails');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		//获取推广人数
		$data['num'] = $User->where('flag = 1 and parent_id = '.$userId)->count();
		$where = 'type = 1 and from_user_id > 0 and flag = 1 and user_id = '.$userId;
		$data['price'] = (float) $UserDetails->where($where)->sum('price');
		$data['detailsList'] = array();
		//获取明细
		$list = $User->where('flag = 1 and parent_id = '.$userId)->field('id,tel')->order('create_time desc')->select();
		foreach ($list as $key=>$val){
			$data['detailsList'][$key]['username'] = $val['tel'];
			$data['detailsList'][$key]['price'] = (float) $UserDetails->where('flag = 1 and type = 1 and from_user_id = '.$val['id'].' and user_id = '.$userId)->getField('SUM(price) as price');
		}
		$this->assign('userId',$userId);
		$this->assign('data',$data);
		$this->display();
	}
	
	/**
	 * 我的地址
	 */
	public function addressList(){
		$Address = M('Address');$School = M('School');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		$list = $Address->where('flag = 1 and user_id = '.$userId)->select();
		foreach($list as $key=>$val){
			$list[$key]['schoolName'] = (string)$School->where('id = '.$val['school_id'])->getField('name');
		}
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	 * 新增地址
	 */
	public function addAddress(){
		$School = M('School');$Province = M('ZhProvince');$City = M('ZhCity');$Area = M('ZhArea');
		//学校信息
		if(S('S_SYSTEM_SCHOOL')){
			$data = S('S_SYSTEM_SCHOOL');
		}else{
			$data = $School->where('flag = 1')->field('id,province,city,area,name')->order('seq asc')->select();
			S('S_SYSTEM_SCHOOL',$data,86400);//30天后过期
		}
		//省市区
		if(S('S_SYSTEM_AREA')){//启用缓存
			$provinceData = S('S_SYSTEM_AREA');
		}else{
			$provinceData = $Province->select();
			foreach($provinceData as $pk=>$pv){
				$cityData = $City->where('province_id = '.$pv['id'])->field('id as cityId,city')->select();
				$provinceData[$pk]['cityList'] = $cityData;
				foreach ($cityData as $ck=>$cv){
					$areaData = $Area->where('city_id = '.$cv['cityId'])->field('id as areaId,area')->select();
					$provinceData[$pk]['cityList'][$ck]['areaList'] = $areaData;
				}
			}
			S('S_SYSTEM_AREA',$provinceData,2592000);//30天后过期
		}
		$this->assign('schoolList',$data);
		$this->assign('cityList',json_encode($provinceData));
		$this->display();
	}
	
	/**
	 * 新增地址FUN
	 */
	public function insertAddress(){
		$Address = M('Address');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		//检查是否超出上限
		$where['user_id'] = $userId;
		$where['flag'] = 1;
		$num = $Address->where($where)->count();
		if($num >= 5){
			$this->ajaxReturn(0,"新增失败，每个用户最多只能添加5个地址信息！",0);
		}
		if($param['mail_type'] == 1){//校园模式
			$data['school_id'] = $param['school_id'];
		}
		$data['province'] = $param['province'];
		$data['city'] = $param['city'];
		$data['area'] = $param['area'];
		$data['address'] = $param['address'];
		$data['contact_name'] = $param['contact_name'];
		$data['contact_tel'] = $param['contact_tel'];
		$data['user_id'] = $userId;
		$data['is_main'] = 1;
		$data['create_time'] = time();
		$Address->where($where)->setField('is_main',0);
		$result = $Address->data($data)->add();
		if($result){
			$this->ajaxReturn(1,'新增成功！',1);
		}else{
			$this->ajaxReturn(0,"新增失败！",0);
		}
	}
	
	/**
	 * 修改地址
	 */
	public function editAddress(){
		$School = M('School');$Province = M('ZhProvince');$City = M('ZhCity');$Area = M('ZhArea');$Address = M('Address');
		$param = $this->param;
		$where['flag'] = 1;
		$where['user_id'] = $_SESSION['HS_USER_ID'];
		$where['id'] = $param['id'];
		$info = $Address->where($where)->find();
		if(empty($info)){
			$this->error('参数错误，未找到该地址！');
		}
		//学校信息
		if(S('S_SYSTEM_SCHOOL')){
			$data = S('S_SYSTEM_SCHOOL');
		}else{
			$data = $School->where('flag = 1')->field('id,province,city,area,name')->order('seq asc')->select();
			S('S_SYSTEM_SCHOOL',$data,86400);//30天后过期
		}
		//省市区
		if(S('S_SYSTEM_AREA')){//启用缓存
			$provinceData = S('S_SYSTEM_AREA');
		}else{
			$provinceData = $Province->select();
			foreach($provinceData as $pk=>$pv){
				$cityData = $City->where('province_id = '.$pv['id'])->field('id as cityId,city')->select();
				$provinceData[$pk]['cityList'] = $cityData;
				foreach ($cityData as $ck=>$cv){
					$areaData = $Area->where('city_id = '.$cv['cityId'])->field('id as areaId,area')->select();
					$provinceData[$pk]['cityList'][$ck]['areaList'] = $areaData;
				}
			}
			S('S_SYSTEM_AREA',$provinceData,2592000);//30天后过期
		}
		$this->assign('data',$info);
		$this->assign('schoolList',$data);
		$this->assign('cityList',json_encode($provinceData));
		$this->display();
	}
	
	/**
	 * 修改地址FUN
	 */
	public function updateAddress(){
		$Address = M('Address');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		$where['id'] = $param['id'];
		$where['user_id'] = $userId;
		$where['flag'] = 1;
		$count = $Address->where($where)->count();
		if($count == 0){
			$this->ajaxReturn(0,"地址不存在或未找到！",0);
		}
		if($param['mail_type'] == 1){//校园模式
			$data['school_id'] = $param['school_id'];
		}else{
			$data['school_id'] = 0;
		}
		$data['province'] = $param['province'];
		$data['city'] = $param['city'];
		$data['area'] = $param['area'];
		$data['address'] = $param['address'];
		$data['contact_name'] = $param['contact_name'];
		$data['contact_tel'] = $param['contact_tel'];
		$data['update_time'] = time();
		if (false !== $Address->where($where)->save($data)) {
			$this->ajaxReturn(1,'修改成功！',1);
		} else {
			$this->ajaxReturn(0,"修改失败！",0);
		}
	}
	
	/**
	 * 删除地址
	 */
	public function delAddress(){
		$Address = M('Address');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		$where['id'] = $param['id'];
		$where['user_id'] = $userId;
		$where['flag'] = 1;
		$count = $Address->where($where)->count();
		if($count == 0){
			$this->ajaxReturn(0,"地址不存在或未找到！",0);
		}
		$ismain = $Address->where('id = '.$param['id'])->getField('is_main');
		if($ismain == 1){//如果删除的是主应用，删除主应用后设置下一条数据为主应用
			$Address->where('user_id = '.$userId.' and flag = 1 and id <>'.$param['id'])->order('id desc')->limit(1)->setField('is_main',1);
		}
		$result = $Address->where('flag = 1 and id = '.$param['id'])->setField('flag',2);
		if($result){
			$this->ajaxReturn(1,'删除成功！',1);
		}else{
			$this->ajaxReturn(0,"删除失败！",0);
		}
	}
	
	/**
	 * 设置默认收货地址
	 */
	public function setDefaultAddress(){
		$Address = M('Address');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		// 只有一个主应用
		$Address->where('user_id = '.$userId)->setField("is_main",0);
		$result = $Address->where('flag = 1 and id = '.$param['id'].' and user_id = '.$userId)->setField('is_main',1);
		if($result){
			$this->ajaxReturn(1,'设置成功！',1);
		}else{
			$this->ajaxReturn(0,"设置失败！",0);
		}
	}
	
	/**
	 * 修改登录密码
	 * 视图
	 */
	public function editPassword(){
		$this->display();
	}
	
	/**
	 * 修改登录密码
	 * 方法
	 */
	public function updatePassword(){
		$User = M('User');$LoginSession = M('LoginSession');
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		if(empty($param['oldpassword']) || empty($param['password'])){
			$this->ajaxReturn(0,'参数错误！',0);
		}
		$info = $User->where("id = ".$userId)->find();
		$oldpassword = md5($param['oldpassword']);
		if($info['password'] != $oldpassword){
			$this->ajaxReturn(0,'原密码错误！',0);
		}
		if($info['password'] == md5($param['password'])){
			$this->ajaxReturn(0,'修改的密码不能和原密码一样！',0);
		}
		$data['password'] = md5($param['password']);
		$data['update_time'] = time();
		$result = $User->where("id = ".$userId)->save($data);
		if($result !== false){
			//删除手机端session
			$LoginSession->where('user_id = '.$_SESSION['HS_USER_ID'])->delete();
			//清空session,返回登录页
			unset($_SESSION['HS_USER_ID']);
			unset($_SESSION['HS_USER_TEL']);
			session(null);
			$this->ajaxReturn(1,'操作成功！',1);
		}else{
			$this->ajaxReturn(0,'操作失败！',0);
		}
	}
	
	/**
	 * 修改密码
	 */
	public function modifyTel(){
		$this->display();
	}
	
	/**
	 * 修改手机号
	 */
	public function editTel(){
		$User = M('User');$LoginSession = M('LoginSession');
		$param = $this->param;
		if(!checkPhone($param['oldTel']) || !checkPhone($param['newTel'])){
			$this->ajaxReturn(0,"参数错误！",0);
		}
		$User_old_tel = $param['oldTel'];
		$User_new_tel = $param['newTel'];
		//判断新手机号是否已经被注册
		$count = $User->where("flag = 1 and tel = '".$param['newTel']."'")->count();//包含虚拟用户
		if($count != 0){
			$this->ajaxReturn(0,"新手机已被注册，请更换手机号！",0);
		}
		$info = $User->where("id = ".$_SESSION['HS_USER_ID'])->find();
		if($param['newTel'] == $param['oldTel'] ){//原手机号和修手机号不能相同
			$this->ajaxReturn(0,"新旧手机号不能相同！",0);
		}
		if($info['tel'] != $param['oldTel']){//验证原手机号是否正确
			$this->ajaxReturn(0,"原手机号填写错误！",0);
		}
		if($param['newTel'] != session('send_code_tel')){
			$this->ajaxReturn(0,"请重新获取验证码！",0);
		}
		if($param['phoneCode'] != session('send_tel_code')){
			$this->ajaxReturn(0,"验证码错误！",0);
		}
		$data['tel'] = $param['newTel'];
		$data['username'] = $param['newTel'];
		$result = $User->where("id = ".$_SESSION['HS_USER_ID'])->save($data);
		if($result !== false){
			//删除手机端session
			$LoginSession->where('user_id = '.$_SESSION['HS_USER_ID'])->delete();
			//退出
			session('tel_code',null);
			session('send_code_time',null);
			$_SESSION['HS_USER_ID'] = NULL;
			$_SESSION['HS_USER_TEL'] = NULL;
			session(null);
			$this->ajaxReturn(1,'修改成功！',1);
		}else{
			$this->ajaxReturn(0,'修改失败',0);
		}
	}
}